#  Copyright (c) 2022. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
#  Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
#  Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
#  Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
#  Vestibulum commodo. Ut rhoncus gravida arcu.

# !/bin/env python
# -*- coding:utf-8
"""
@author:xingz
@file:demo.py
@time:2022/03/14
"""

ring_pg_map = {
    'iso1_nch_ldmhs_45v': {('DIFF', 0): [[(-12015, -11735), (198245, -11235)], [(-12015, -11735), (-11515, 26465)], [(197745, -11735), (198245, 26465)], [(-12015, 25965), (198245, 26465)]],
                           ('NIMP', 0): [[(-12195, -11915), (198425, -11055)], [(-12195, -11915), (-11335, 26645)], [(197565, -11915), (198425, 26645)], [(-12195, 25785), (198425, 26645)]],
                           ('SDNW', 0): [[(-12515, -12235), (198745, -10735)], [(-12515, -12235), (-11015, 26965)], [(197245, -12235), (198745, 26965)], [(-12515, 25465), (198745, 26965)]],
                           ('MET1', 0): [[(-12015, -11735), (198245, -11235)], [(-12015, -11735), (-11515, 26465)], [(197745, -11735), (198245, 26465)], [(-12015, 25965), (198245, 26465)]],
                           ('CONT', 0): [[(-11335, -11595), (197565, -11375)], [(-11875, -11075), (-11655, 25805)], [(197885, -11075), (198105, 25805)], [(-11335, 26105), (197565, 26325)]]},

    'iso2_nch_ldmhs_45v': {('DIFF', 0): [[(-6665, -6385), (192895, -5385)], [(-6665, -6385), (-5665, 21115)], [(191895, -6385), (192895, 21115)], [(-6665, 20115), (192895, 21115)]],
                           ('PIMP', 0): [[(-6845, -6565), (193075, -5205)], [(-6845, -6565), (-5485, 21295)], [(191715, -6565), (193075, 21295)], [(-6845, 19935), (193075, 21295)]],
                           ('SDPW', 0): [[(-7215, -6935), (193445, -4835)], [(-7215, -6935), (-5115, 21665)], [(191345, -6935), (193445, 21665)], [(-7215, 19565), (193445, 21665)]],
                           ('DWELL', 0): [[(-6855, -6575), (193085, -5195)], [(-6855, -6575), (-5475, 21305)], [(191705, -6575), (193085, 21305)], [(-6855, 19925), (193085, 21305)]],
                           ('MET1', 0): [[(-6665, -6385), (192895, -5385)], [(-6665, -6385), (-5665, 21115)], [(191895, -6385), (192895, 21115)], [(-6665, 20115), (192895, 21115)]],
                           ('CONT', 0): [[(-5460, -5995), (191690, -5775)], [(-6275, -5200), (-6055, 19930)], [(192285, -5200), (192505, 19930)], [(-5460, 20505), (191690, 20725)]]},

    'iso3_nch_ldmhs_45v': {('DIFF', 0): [[(-8165, -7885), (194395, -5885)], [(-8165, -7885), (-6165, 22615)], [(192395, -7885), (194395, 22615)], [(-8165, 20615), (194395, 22615)]],
                           ('NIMP', 0): [[(-8345, -8065), (194575, -5705)], [(-8345, -8065), (-5985, 22795)], [(192215, -8065), (194575, 22795)], [(-8345, 20435), (194575, 22795)]],
                           ('SDNW', 0): [[(-8665, -8385), (194895, -4885)], [(-8665, -8385), (-5165, 23115)], [(191395, -8385), (194895, 23115)], [(-8665, 19615), (194895, 23115)]],
                           ('NBL', 0): [[(-8665, -8385), (194895, 23115)]],
                           ('DNWELL', 0): [[(-10165, -9885), (196395, 215)], [(-10165, -9885), (-65, 24615)], [(186295, -9885), (196395, 24615)], [(-10165, 14515), (196395, 24615)]],
                           ('NDT', 0): [[(-8665, -8385), (194895, -5385)], [(-8665, -8385), (-5665, 23115)], [(191895, -8385), (194895, 23115)], [(-8665, 20115), (194895, 23115)]],
                           ('PCHSTBLK', 0): [[(-12665, -12385), (198895, 27115)]], ('TGOX50', 0): [[(-10165, -9885), (196395, 24615)]],
                           ('MET1', 0): [[(-8165, -7885), (194395, -5885)], [(-8165, -7885), (-6165, 22615)], [(192395, -7885), (194395, 22615)], [(-8165, 20615), (194395, 22615)]],
                           ('CONT', 0): [[(-5930, -6995), (192160, -6775)], [(-7275, -5670), (-7055, 20400)], [(193285, -5670), (193505, 20400)], [(-5930, 21505), (192160, 21725)]]}
}

bb_list = [[-14540, -14595, -2310, 52205], [-2310, -14595, -180, 52205],
           [-180, -14595, 820, 52205], [820, -14595, 1360, 52205], [1360, -14595, 1900, 52205], [1900, -14595, 2900, 52205],
           [2900, -14595, 5030, 52205], [5030, -14595, 17260, 52205]]

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

# define Matplotlib figure and axis
fig, ax = plt.subplots()
ax.plot([10, 10], [10, 10])

# add rectangle to plot
# for bb, name in zip(bb_list, ['d1l', 'd1r', 'g1', 's1l', 's1r', 'g2', 'd2l', 'd2r']):
#     ax.add_patch(Rectangle((bb[0], bb[1]), bb[2] - bb[0], bb[3] - bb[1], fill=False))
#     plt.text((bb[0] + bb[2]) // 2, (bb[1] + bb[3]) // 2, s=name)
x = [-619385.0, -619385.0, -487305.0, -487305.0, -526850.0, -526850.0, -619385.0]

y = [155085.0, 179585.0, 179585.0, 170600.0, 170600.0, 155085.0, 155085.0]
# color_map = {'iso1_nch_ldmhs_45v': 'red',
#              'iso2_nch_ldmhs_45v': 'blue',
#              'iso3_nch_ldmhs_45v': 'green'
#              }
#
# unit_ring = 'iso3_nch_ldmhs_45v'
# for ring_type, layer_bb_dict in ring_pg_map.items():
#     color = color_map[ring_type]
#     for layer, bb_list in layer_bb_dict.items():
#         fill = False
#         if layer != ('DIFF', 0): continue
#         # if ring_type == unit_ring:
#         #     fill = True
#
#         for bb in bb_list:
#             ax.add_patch(Rectangle((bb[0][0], bb[0][1]), bb[1][0] - bb[0][0], bb[1][1] - bb[0][1], fill=fill, color=color))
# display plot

file = "../../agent_shape.csv"

ring_map = {
    'TRANSISTOR|vs_in|SUB|sub_ring_default': [(0, 97820), (8500, 97820), (17000, 97820), (25500, 97820), (34000, 97820), (0, 109320), (0, 110820), (0, 116820), (2600, 116820), (0, 118820), (0, 56000),
                                              (8500, 56000), (0, 67000), (9000, 67000), (27000, 67000), (36000, 67000), (54000, 67000), (63000, 67000), (0, 78000), (0, 79220), (0, 82220),
                                              (2500, 82220), (6500, 82220), (0, 88220), (2500, 88220), (5000, 88220), (0, 91220), (7000, 91220)],
    'TRANSISTOR|vh_in|B|body_pch_svt_5p0v': [(0, 97820), (8500, 97820), (17000, 97820), (25500, 97820), (34000, 97820), (0, 109320), (0, 110820), (0, 116820), (2600, 116820), (0, 118820)],
    'TRANSISTOR|vs_in|B|body_nch_svt_5p0v': [(0, 56000), (8500, 56000), (0, 67000), (9000, 67000), (27000, 67000), (36000, 67000), (54000, 67000), (63000, 67000), (0, 78000), (0, 79220), (0, 82220),
                                             (2500, 82220), (6500, 82220), (0, 88220), (2500, 88220), (5000, 88220), (0, 91220), (7000, 91220)],
    'CAPACITOR|vs_in|SUB|sub_ring_default': [(0, 33000)],
    'RESISTOR|vs_in|SUB|sub_ring_default': [(0, 0), (13000, 0)]}

# self_bb_lst = [(-274575, -304805, -81795, 68195), (-77795, -304805, 114985, 68195),
#                (-65715, 352925, 127065, 725925), (547850, 66415, 480950, -324225)]
# target_bb_list = [(-262495, 352925, -69715, 725925), (476950, 66415, 410050, -324225)
#     , (468010, 752475, 401110, 361835), (538910, 752475, 472010, 361835)]
ret_bb_list = [[1000, -42270, 3000, 47730], [89000, -42270, 91000, 47730], [3000, 45730, 89000, 47730], [3000, -42270, 89000, -40270]]


# for ring, device_loc_list in ring_map.items():
#     ring_x_list = [i[0] for i in device_loc_list]
#     ring_y_list = [i[1] for i in device_loc_list]
#     ring_bb = (min(ring_x_list), min(ring_y_list), max(ring_x_list) + 2000, max(ring_y_list) + 2000)
#     ax.add_patch(Rectangle(ring_bb[:2], ring_bb[2] - ring_bb[0], ring_bb[3] - ring_bb[1], fill=False))
#
#     for point in device_loc_list:
#         ax.add_patch(Rectangle(point, 1000, 1000, fill=False))

# for bb in self_bb_lst:
#     ax.add_patch(Rectangle(bb[:2], bb[2] - bb[0], bb[3] - bb[1], fill=False))
#     pass
#
# for bb in target_bb_list:
#     ax.add_patch(Rectangle(bb[:2], bb[2] - bb[0], bb[3] - bb[1], fill=False, color='red'))


def bb_cut_bb(b1: iter, b2: iter):
    def satisfied(bb):
        if bb[3] - bb[1] <= 0:
            return False
        if bb[2] - bb[0] <= 0:
            return False
        return True

    l = b1[0], b1[1], b2[0], b1[3]
    r = b2[2], b1[1], b1[2], b1[3]
    t = b1[0], b2[3], b1[2], b1[3]
    b = b1[0], b1[1], b1[2], b2[1]
    ret = [l, r, t, b]
    ret = [bb for bb in ret if satisfied(bb)]
    return ret


def bb_cut_bbs(b1, b2_lst):
    frontier = [b1]
    cutt_lst = []
    for b2 in b2_lst:
        cutt_lst = []
        while frontier:
            b1 = frontier.pop()
            b_c_lst = bb_cut_bb(b1, b2)
            if b_c_lst:
                if b_c_lst[0] == b1:
                    cutt_lst.append(b1)
                else:
                    frontier.extend(b_c_lst)
        frontier = cutt_lst
    return cutt_lst


bb1 = [0, 0, 80, 20]
bb2 = [20, 30, 30, 60]
bb_list = [bb1, bb2]

fall_bb = [min([bb1[0], bb1[2], bb2[0], bb2[2]]),
           min([bb1[1], bb1[3], bb2[1], bb2[3]]),
           max([bb1[0], bb1[2], bb2[0], bb2[2]]),
           max([bb1[1], bb1[3], bb2[1], bb2[3]])]

extend_bb1_y = [bb1[0], min(bb1[1], bb2[1]), bb1[2], max(bb1[3], bb2[3])]
extend_bb1_x = [min(bb1[0], bb2[0]), bb1[1], max(bb1[2], bb2[2]), bb1[3]]
extend_bb2_y = [bb2[0], min(bb1[1], bb2[1]), bb2[2], max(bb1[3], bb2[3])]
extend_bb2_x = [min(bb1[0], bb2[0]), bb2[1], max(bb1[2], bb2[2]), bb2[3]]
bb_list_extend = [extend_bb1_y, extend_bb1_x, extend_bb2_y, extend_bb2_x]


def cal_rect_space(bb1, bb2):
    b1_x0, b1_y0, b1_x1, b1_y1 = bb1
    b2_x0, b2_y0, b2_x1, b2_y1 = bb2
    left = b2_x1 < b1_x0
    right = b2_x0 > b1_x1
    bottom = b2_y1 < b1_y0
    top = b2_y0 > b1_y1
    dist = None
    direction = None
    v = None
    if top and left:
        pass
    elif top and right:
        pass
    elif bottom and left:
        pass
    elif bottom and right:
        pass
    elif top:
        dist = b2_y0 - b1_y1
        v = (b1_x1, b1_y1, b1_x1, b1_y1 + dist)
        direction = 'top'

    elif bottom:
        dist = b1_y0 - b2_y1
        v = (b1_x0, b1_y0, b1_x0, b1_y0 - dist)
        direction = 'bottom'
    elif left:
        dist = b1_x0 - b2_x1
        v = (b1_x0, b1_y0, b1_x0 - dist, b1_y0)
        direction = 'left'

    elif right:
        dist = b2_x0 - b1_x1
        v = (b1_x1, b1_y1, b1_x1 + dist, b1_y1)
        direction = 'right'
    return dist, direction


def reshape_bb(bb, direction, scaling_value):
    if direction is None:
        return bb
    if direction == "top":
        bb = bb[0], bb[1] + scaling_value, bb[2], bb[3]
    elif direction == "bottom":
        bb = bb[0], bb[1], bb[2], bb[3] + scaling_value
    elif direction == "left":
        bb = bb[0], bb[1], bb[2] + scaling_value, bb[3]
    elif direction == 'right':
        bb = bb[0] + scaling_value, bb[1], bb[2], bb[3]
    return bb


def area(bb):
    return (bb[2] - bb[0]) * (bb[3] - bb[1])
    pass


dr__space__v1__min = 260

bbs = ([31575, 301640, 32355, 336220], [32505, 289895, 33285, 324475], (32395, 289895, 33285, 324475))

bb1 = bbs[0]
bb2 = bbs[1]
new_bb = bbs[2]
cc = sorted([bb1, bb2], key=lambda x: area(x))[0]

space, direction = cal_rect_space(bb1, bb2)

direction = 'left'

scaling_valur = dr__space__v1__min - space

new_bb = reshape_bb(cc, direction, -scaling_valur)
print(scaling_valur)
from zx_demo.demo.demo6 import randomcolor
bbs = ((54100, -31130, 54600, 26870), (8380, -31130, 8880, 26870), (8880, -31630, 54100, -31130), (8880, -31630, 54100, -31130))

for bb in bbs:
    print(bb)
    bb = bb[0]+1, bb[1]+1, bb[2]-1, bb[3]-1
    ax.add_patch(Rectangle(bb[:2], (bb[2] - bb[0]), (bb[3] - bb[1]), fill=True,alpha=0.5 ,linewidth=1, color=randomcolor()))

plt.show()
# [i.f_get_bb(self.currentBlock, LayoutHome().df) for i in cust1_generator_lst]
